package br.com.mftc.efitness.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Util {

	private static SimpleDateFormat formatoDataSimples;
	private static Calendar calendar;
	private static Date data;

	public static boolean contemCaracteresEspeciais(String string) {
		String[] caracteres = { "\"", "!", "@", "#", "$", "%", "&", "*", "(", ")", "-", "_", "=", "+", "\\", "|", "<", ">", "?", "/" };
		for (int i = 0; i < caracteres.length; i++) {
			if (string.contains(caracteres[i])) {
				return true;
			}
		}
		return false;
	}

	public static boolean validarEmail(String email) {
		boolean valido = false;
		if (email != null && email.length() > 0) {
			String expressao = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
			Pattern pattern = Pattern.compile(expressao, Pattern.CASE_INSENSITIVE);
			Matcher matcher = pattern.matcher(email);
			if (matcher.matches()) {
				valido = true;
			}
		}
		return valido;
	}

	public static String dataSimplesParaTexto(Date data) {
		return formatoDataSimples.format(data);
	}

	public static Date textoParaDataSimples(String texto) throws ParseException {
		return formatoDataSimples.parse(texto);
	}

	public static Date agora() {
		if (data == null) {
			data = new Date();
		} else {
			data.setTime(System.currentTimeMillis());
		}
		return data;
	}

	public static Date inicioData(Date data) {
		calendar = Calendar.getInstance();
		calendar.setTime(data);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		return new Date(calendar.getTimeInMillis());
	}

	public static Date fimData(Date data) {
		calendar = Calendar.getInstance();
		calendar.setTime(data);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		return new Date(calendar.getTimeInMillis());
	}

	public static String criptografarSenhaMD5(String password) throws NoSuchAlgorithmException, UnsupportedEncodingException {
		return String.format("%32x", new BigInteger(1, MessageDigest.getInstance("MD5").digest(password.getBytes("UTF-8"))));
	}

	public static String removerMascara(String texto) {
		return texto.replace("-", "").replace(".", "").replace("(", "").replace(")", "").replace("/", "");
	}

	public static boolean validarCnpj(String cnpj) {
		cnpj = removerMascara(cnpj);

		Integer k = 0, dig = 0, soma = 0;
		String strNum = "";

		try {
			strNum = cnpj.substring(0, cnpj.length() - 2);
		} catch (StringIndexOutOfBoundsException e) {
			return false;
		}
		for (int i = 1; i <= 2; i++) {
			k = 2;
			soma = 0;
			for (int j = strNum.length(); j > 0; j--) {
				try {
					soma += Integer.parseInt(strNum.substring((j - 1), j)) * k;
				} catch (StringIndexOutOfBoundsException e) {
					return false;
				}
				k++;
				if (k > 9) {
					k = 2;
				}
			}
			dig = 11 - soma % 11;
			if (dig >= 10) {
				dig = 0;
			}
			strNum += dig.toString();
		}
		return strNum.compareTo(cnpj) == 0 ? true : false;
	}

	public static boolean validarCpf(String cpf) {
		cpf = removerMascara(cpf);

		Integer k = 0, dig = 0, soma = 0;
		String strNum = "";

		try {
			strNum = cpf.substring(0, cpf.length() - 2);
		} catch (StringIndexOutOfBoundsException e) {
			return false;
		}
		for (int i = 1; i <= 2; i++) {
			k = 2;
			soma = 0;
			for (int j = strNum.length(); j > 0; j--) {
				try {
					soma += Integer.parseInt(strNum.substring(j - 1, j)) * k;
				} catch (StringIndexOutOfBoundsException e) {
					return false;
				}
				k++;
			}
			dig = 11 - soma % 11;
			if (dig >= 10) {
				dig = 0;
			}
			strNum += dig.toString();
		}
		return strNum.compareTo(cpf) == 0 ? true : false;
	}

	public static void compactarParaZip(String arquivoZip, String arquivoASerZipado) throws IOException {
		int TAMANHO_BUFFER = 4096;
		int cont;

		byte[] dados = new byte[TAMANHO_BUFFER];

		BufferedInputStream bufferedInputStream = null;
		FileInputStream fileInputStream = null;
		FileOutputStream fileOutputStream = null;
		ZipOutputStream zipOutputStream = null;
		ZipEntry zipEntry = null;

		try {
			fileOutputStream = new FileOutputStream(new File(arquivoZip));
			zipOutputStream = new ZipOutputStream(new BufferedOutputStream(fileOutputStream));
			File file = new File(arquivoASerZipado);
			fileInputStream = new FileInputStream(file);
			bufferedInputStream = new BufferedInputStream(fileInputStream, TAMANHO_BUFFER);
			zipEntry = new ZipEntry(file.getName());
			zipOutputStream.putNextEntry(zipEntry);
			while ((cont = bufferedInputStream.read(dados, 0, TAMANHO_BUFFER)) != -1) {
				zipOutputStream.write(dados, 0, cont);
			}
			bufferedInputStream.close();
			zipOutputStream.close();
		} catch (IOException e) {
			throw new IOException(e.getMessage());
		}
	}

	public String gerarNovaSenha() {
		String[] carct = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
				"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
				"m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x",
				"y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
				"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
				"W", "X", "Y", "Z" };
		
		String senha = "";
		
		for (int x = 0; x < 10; x++) {
			senha += carct[(int) (Math.random() * carct.length)];
		}
		
		return senha;
	}
}
